<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="giit.computerbase.server.SearchServer" %>
<%@ page import="giit.computerbase.server.RegisterCodeServer" %>
<%@ page import="giit.computerbase.server.UserServer" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Manager</title>

    <!-- Bootstrap core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="css/signin.css" rel="stylesheet">

    <style>
        .sidebar {
            display: none;
            border: 0;
        }

        @media (min-width: 768px) {
            .sidebar {
                position: fixed;
                top: 51px;
                bottom: 0;
                left: 0;
                z-index: 1000;
                display: block;
                padding: 20px;
                overflow-x: hidden;
                overflow-y: auto; /* Scrollable contents if viewport is shorter than content. */
                background-color: #f5f5f5;
                border-right: 1px solid #eee;
            }
        }

        .nav-sidebar {
            margin-right: -21px; /* 20px padding + 1px border */
            margin-bottom: 20px;
            margin-left: -20px;
        }

        .nav-sidebar > li > a {
            padding-right: 20px;
            padding-left: 20px;
        }

        .nav-sidebar > .active > a,
        .nav-sidebar > .active > a:hover,
        .nav-sidebar > .active > a:focus {
            color: #fff;
            background-color: #428bca;
        }


    </style>

</head>

<%
    String user = (String) request.getSession().getAttribute("user");
    boolean isManager = (boolean) request.getSession().getAttribute("manager");
    if (user == null || user.equals("") || !isManager) {
        response.sendRedirect("/index");
    }
%>

<body>
<!-- page header start-->
<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
                    aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="index">主页</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
            <ul class="nav navbar-nav navbar-right">
                <%
                    user = (String) request.getSession().getAttribute("user");
                    if (user == null || user.equals("")) {
                %>
                <li><a href="login.jsp">登录</a></li>
                <li><a href="register.jsp">注册</a></li>
                <%
                } else {
                %>
                <li><a href=""><%=request.getSession().getAttribute("user")%>
                </a></li>
                <li><a href="edit.jsp">发新闻稿</a></li>
                <%
                    if ((boolean) request.getSession().getAttribute("manager")) {
                %>
                <li><a href="manager.jsp">管理</a></li>
                <%
                    }
                %>
                <li><a href="logout">退出</a></li>
                <%
                    }
                %>
            </ul>
        </div><!--/.nav-collapse -->
    </div>
</nav>
<!-- page header end-->


<div class="container" style="padding: 88px 0 0">

    <div class="row">

        <div class="col-sm-3 col-md-2 sidebar">
            <ul class="nav nav-sidebar">
                <li class="active"><a href="#">Overview <span class="sr-only">(current)</span></a></li>
                <li><a href="#user">用户管理</a></li>
                <li><a href="#recod">帖子管理</a></li>
                <li><a href="#code">帖子管理</a></li>
            </ul>
        </div>

        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">

            <br><br>
            <div id="user" class="panel panel-info">
                <div class="panel-heading" style="text-align: center">用户密码修改</div>

                <div class="panel-body">
                    <div>
                        <p style="font-size:20px;text-align: center" ;>输入要修改的用户名及新密码:</p>
                    </div>
                    <form action="change" method="POST">
                        <div style="text-align: center">
                            <label class="control-label" style="padding-top: auto">用&nbsp;户&nbsp;名&nbsp;&nbsp;&nbsp;:
                                <input type="text" class="span3" name="username" style="height: auto"></label>
                        </div>
                        <div style="text-align: center">
                            <label class="control-label" style="padding-top: auto">密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码&nbsp;&nbsp;&nbsp;:
                                <input type="password" class="span3" name="password" style="height: auto"></label>
                        </div>
                        <div style="text-align: center">
                            <label class="control-label" style="padding-top: auto">再次输入&nbsp;:
                                <input type="password" class="span3" name="again" style="height: auto"></label>

                            <div>
                                <button type="submit" class="btn btn-primary pull-right">修改</button>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="panel-body">
                    <table class="table">
                        <thead>
                        <tr>
                            <th>序号</th>
                            <th>用户名</th>
                            <th>用户类型</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <%
                                UserServer userServer = new UserServer();
                                ArrayList<HashMap<String, Object>> result = userServer.getUsers();
                                Iterator<HashMap<String, Object>> iterator = result.iterator();
                                HashMap<String, Object> temp;
                                int count = 0;
                                while (iterator.hasNext()) {
                                    count++;
                                    temp = iterator.next();
                            %>
                            <td><%=count%>
                            </td>
                            <td><%=temp.get("name")%>
                            </td>
                            <td><%=temp.get("user_type_id").equals("type_01") ? "普通用户" : "管理员"%>
                            </td>
                            <td>
                                <!-- <button  class="btn btn-primary">Delete</button>-->
                                <button type="button" data-target="#delUser<%=count%>" data-toggle="modal"
                                        class="btn-danger">删除
                                </button>
                                <div id="delUser<%=count%>" class="modal" tabindex="-1" role="dialog" aria-labelledby="delete">
                                    <form action="deleteuser" method="POST"
                                          id="<%=temp.get("name")%>">
                                        <input hidden="hidden" name="name" value="<%=temp.get("name")%>">
                                        <div class="modal-dialog" role="document">
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <button type="button" class="close" data-dismiss="modal"
                                                            aria-label="Close"><span
                                                            aria-hidden="true">&times;</span>
                                                    </button>
                                                    <h5>Delete record</h5><!-- modal title -->
                                                </div>
                                                <div class="modal-body">
                                                    <p style="font-size:20px;"> 确定删除用户 <%=temp.get("name")%>
                                                        ? </p>
                                                    <!-- modal content-->
                                                </div>
                                                <div class="modal-footer">
                                                    <button type="button" class="btn btn-success"
                                                            data-dismiss="modal"
                                                            onclick="s(document.getElementById('<%=temp.get("name")%>'))">
                                                        Yes
                                                    </button>
                                                    <button type="button" class="btn btn-info" data-dismiss="modal">
                                                        No
                                                    </button>
                                                </div>
                                            </div>
                                        </div>
                                    </form>
                                </div>

                            </td>
                        </tr>
                        <%
                            }
                        %>
                        </tbody>
                    </table>
                </div>

                <br><br>
                <div id="recod" class="panel panel-warning">
                    <div class="panel-heading" style="text-align: center">新闻稿管理</div>

                    <div class="panel-body">
                        <table class="table">
                            <thead>
                            <tr>
                                <th>序号</th>
                                <th>标题</th>
                                <th>撰稿人</th>
                                <th>管理</th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr>
                                <%
                                    SearchServer searchServer = new SearchServer();
                                    result = searchServer.search("");
                                    iterator = result.iterator();
                                    count = 0;
                                    while (iterator.hasNext()) {
                                        count++;
                                        temp = iterator.next();
                                %>
                                <td><%=count%>
                                </td>
                                <td><%=temp.get("title")%>
                                </td>
                                <td><%=temp.get("contributor")%>
                                </td>
                                <td>
                                    <!-- <button  class="btn btn-primary">Delete</button>-->
                                    <button type="button" data-target="#del<%=count%>" data-toggle="modal" class="btn-danger">删除
                                    </button>
                                    <div id="del<%=count%>" class="modal" tabindex="-1" role="dialog" aria-labelledby="delete">
                                        <form action="delete" method="POST"
                                              id="<%=temp.get("title")%>">
                                            <input hidden="hidden" name="title"
                                                   value="<%=URLEncoder.encode(temp.get("title").toString())%>">
                                            <div class="modal-dialog" role="document">
                                                <div class="modal-content">
                                                    <div class="modal-header">
                                                        <button type="button" class="close" data-dismiss="modal"
                                                                aria-label="Close"><span
                                                                aria-hidden="true">&times;</span>
                                                        </button>
                                                        <h5>Delete record</h5><!-- modal title -->
                                                    </div>
                                                    <div class="modal-body">
                                                        <p style="font-size:20px;"> 确定删除新闻稿 <%=temp.get("title")%>
                                                            ? </p>
                                                        <!-- modal content-->
                                                    </div>
                                                    <div class="modal-footer">
                                                        <button type="button" class="btn btn-success"
                                                                data-dismiss="modal"
                                                                onclick="s(document.getElementById('<%=temp.get("title")%>'))">
                                                            Yes
                                                        </button>
                                                        <button type="button" class="btn btn-info" data-dismiss="modal">
                                                            No
                                                        </button>
                                                    </div>
                                                </div>
                                            </div>
                                        </form>
                                    </div>

                                </td>
                            </tr>
                            <%
                                }
                            %>
                            </tbody>
                        </table>
                    </div>
                </div>

                <br><br>
                <div id="code" class="panel panel-warning">
                    <div class="panel-heading" style="text-align: center">注册码管理</div>

                    <div class="panel-body">
                        <div>
                            <p style="font-size:20px;text-align: center" ;>输入要生成的注册码数量:</p>
                        </div>
                        <form action="registercode" method="POST">
                            <input hidden="hidden" name="operation" value="add">
                            <div style="text-align: center">
                                <label class="control-label" style="padding-top: auto">数 量 :
                                    <input type="text" class="span3" name="count" style="height: auto"
                                           pattern="[0-9]{1,}" placeholder="默认为1"></label>
                            </div>
                            <div>
                                <button type="submit" class="btn btn-primary pull-right">生成</button>
                            </div>
                        </form>
                    </div>
                </div>

                <div class="panel-body">
                    <table class="table">
                        <thead>
                        <tr>
                            <th>序号</th>
                            <th>注册码</th>
                            <th>管理</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <%
                                RegisterCodeServer registerCodeServer = new RegisterCodeServer();
                                result = registerCodeServer.findAll();
                                iterator = result.iterator();
                                count = 0;
                                while (iterator.hasNext()) {
                                    count++;
                                    temp = iterator.next();
                            %>
                            <td><%=count%>
                            </td>
                            <td><%=temp.get("code")%>
                            </td>
                            <td>
                                <!-- <button  class="btn btn-primary">Delete</button>-->
                                <button type="button" data-target="#delcode<%=count%>" data-toggle="modal" class="btn-danger">删除
                                </button>
                                <div id="delcode<%=count%>" class="modal" tabindex="-1" role="dialog" aria-labelledby="delete">
                                    <form action="registercode" method="POST"
                                          id="<%=temp.get("code")%>">
                                        <input hidden="hidden" name="operation" value="delete">
                                        <input hidden="hidden" name="code" value="<%=temp.get("code")%>">
                                        <div class="modal-dialog" role="document">
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <button type="button" class="close" data-dismiss="modal"
                                                            aria-label="Close"><span
                                                            aria-hidden="true">&times;</span>
                                                    </button>
                                                    <h5>Delete record</h5><!-- modal title -->
                                                </div>
                                                <div class="modal-body">
                                                    <p style="font-size:20px;"> 确定删除注册码 <%=temp.get("code")%> ？ </p>
                                                    <!-- modal content-->
                                                </div>
                                                <div class="modal-footer">
                                                    <button type="button" class="btn btn-success"
                                                            data-dismiss="modal"
                                                            onclick="s(document.getElementById('<%=temp.get("code")%>'))">
                                                        Yes
                                                    </button>
                                                    <button type="button" class="btn btn-info" data-dismiss="modal">
                                                        No
                                                    </button>
                                                </div>
                                            </div>
                                        </div>
                                    </form>
                                </div>

                            </td>
                        </tr>
                        <%
                            }
                        %>
                        </tbody>
                    </table>
                </div>
            </div>

        </div>

    </div>

</div> <!-- /container -->


<script src="js/jquery-3.3.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>

<script>
    function s(f) {
        f.submit();
    }
</script>
</body>
</html>
